package org.jeecg.modules.demo.patent.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo.timeline.service.IPatentTimelineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

/**
 * 时间脉络接口兼容控制器（提供前端所需旧路径）
 */
@Tag(name = "专利时间脉络兼容")
@RestController
@RequestMapping("/patent/patent")
@Slf4j
public class PatentTimelineCompatController {

    @Autowired
    private IPatentTimelineService patentTimelineService;

    /**
     * 获取专利时间脉络数据（新版本）
     */
    @Operation(summary = "获取专利时间脉络数据")
    @GetMapping(value = "/timeline/v2")
    public Result<Map<String, Object>> getPatentTimelineV2(
            @RequestParam(name = "startDate", required = false) String startDate,
            @RequestParam(name = "endDate", required = false) String endDate,
            @RequestParam(name = "eventType", required = false) String eventType,
            @RequestParam(name = "inventor", required = false) String inventor,
            @RequestParam(name = "institution", required = false) String institution) {
        try {
            log.info("开始获取专利时间脉络数据V2...");
            List<Map<String, Object>> timelineDataList = patentTimelineService.getTimelineData(startDate, endDate, eventType, inventor, institution);
            Map<String, Object> statisticsMap = patentTimelineService.getTimelineStatistics(startDate, endDate, eventType, inventor, institution);
            
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("timelineData", timelineDataList);
            resultMap.put("statistics", statisticsMap);
            
            log.info("获取专利时间脉络数据V2成功，数据量: {}", timelineDataList.size());
            return Result.OK(resultMap);
        } catch (Exception e) {
            log.error("获取专利时间脉络数据V2失败", e);
            return Result.error("获取专利时间脉络数据V2失败: " + e.getMessage());
        }
    }

    /**
     * 生成时间脉络数据
     */
    @Operation(summary = "生成时间脉络数据")
    @PostMapping(value = "/timeline/generate")
    public Result<String> generateTimelineData() {
        try {
            log.info("开始生成时间脉络数据...");
            patentTimelineService.generateTimelineData();
            log.info("时间脉络数据生成完成");
            return Result.OK("时间脉络数据生成成功！");
        } catch (Exception e) {
            log.error("生成时间脉络数据失败", e);
            return Result.error("生成时间脉络数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取年份分布统计
     */
    @Operation(summary = "获取年份分布统计")
    @GetMapping(value = "/timeline/year-distribution")
    public Result<List<Map<String, Object>>> getYearDistribution(
            @RequestParam(name = "startDate", required = false) String startDate,
            @RequestParam(name = "endDate", required = false) String endDate) {
        try {
            List<Map<String, Object>> yearDistributionList = patentTimelineService.getYearDistribution(startDate, endDate);
            return Result.OK(yearDistributionList);
        } catch (Exception e) {
            log.error("获取年份分布统计失败", e);
            return Result.error("获取年份分布统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取机构统计
     */
    @Operation(summary = "获取机构统计")
    @GetMapping(value = "/timeline/institution-stats")
    public Result<List<Map<String, Object>>> getInstitutionStats(
            @RequestParam(name = "startDate", required = false) String startDate,
            @RequestParam(name = "endDate", required = false) String endDate) {
        try {
            List<Map<String, Object>> institutionStatsList = patentTimelineService.getInstitutionStats(startDate, endDate);
            return Result.OK(institutionStatsList);
        } catch (Exception e) {
            log.error("获取机构统计失败", e);
            return Result.error("获取机构统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取发明人统计
     */
    @Operation(summary = "获取发明人统计")
    @GetMapping(value = "/timeline/inventor-stats")
    public Result<List<Map<String, Object>>> getInventorStats(
            @RequestParam(name = "startDate", required = false) String startDate,
            @RequestParam(name = "endDate", required = false) String endDate) {
        try {
            List<Map<String, Object>> inventorStatsList = patentTimelineService.getInventorStats(startDate, endDate);
            return Result.OK(inventorStatsList);
        } catch (Exception e) {
            log.error("获取发明人统计失败", e);
            return Result.error("获取发明人统计失败: " + e.getMessage());
        }
    }

    /**
     * 测试数据库连接
     */
    @Operation(summary = "测试数据库连接")
    @GetMapping(value = "/test")
    public Result<String> testConnection() {
        try {
            log.info("测试数据库连接...");
            return Result.OK("数据库连接正常！");
        } catch (Exception e) {
            log.error("数据库连接测试失败", e);
            return Result.error("数据库连接测试失败: " + e.getMessage());
        }
    }

    /**
     * 清理时间脉络数据
     */
    @Operation(summary = "清理时间脉络数据")
    @PostMapping(value = "/timeline/clear")
    public Result<String> clearTimelineData() {
        try {
            log.info("开始清理时间脉络数据...");
            patentTimelineService.clearTimelineData();
            log.info("时间脉络数据清理完成");
            return Result.OK("时间脉络数据清理成功！");
        } catch (Exception e) {
            log.error("清理时间脉络数据失败", e);
            return Result.error("清理时间脉络数据失败: " + e.getMessage());
        }
    }
}


